package com.example.neteasemusic.welcome

import android.annotation.SuppressLint
import android.content.Intent
import android.util.Log
import android.view.LayoutInflater
import androidx.viewbinding.ViewBinding
import com.bumptech.glide.Glide
import com.example.lib_data.base.activity.BaseActivity
import com.example.lib_data.base.fragment.BaseFragment
import com.example.lib_data.bmob.AdvManager
import com.example.lib_data.utils.PXDTimer
import com.example.lib_data.utils.navigateTo
import com.example.neteasemusic.MainActivity
import com.example.neteasemusic.R
import com.example.neteasemusic.databinding.FragmentAdvertisementBinding

class AdvertisementFragment: BaseFragment() {
    private lateinit var mTimer: PXDTimer
    override fun getLayoutBinding(inflater: LayoutInflater): ViewBinding? {
        return FragmentAdvertisementBinding.inflate(inflater)
    }

    override fun initData() {
        val binding = mBinding as FragmentAdvertisementBinding
        /**初始化跳转按钮点击时间*/
        binding.tvJump.setOnClickListener {
            jump()
        }

        /**
         * 加载图片
         */
        val adv = AdvManager.instance.getAdv()
        adv?.let {
            //加载图片
            Glide.with(this).load(it.url).into(binding.ivAdvertisement)
        }
    }

    @SuppressLint("SetTextI18n")
    override fun onResume() {
        super.onResume()
        val binding = mBinding as FragmentAdvertisementBinding
        binding.tvJump.text = "跳转 5"
        //倒计时
        mTimer = PXDTimer()
            .timeInterval(1000)
            .order(PXDTimer.Order.DOWN)
            .startValue(5)
            .setCallback { pxdTimer,value ->
                binding.tvJump.text = "跳转 $value"
                if (value == 0){//跳转到主页面
                   jump()
                }
            }
        mTimer.start()
    }

    /**实现跳转*/
    fun jump(){
        mTimer.stop()
        //requireContext().applicationContext.startActivity(Intent(requireContext().applicationContext,MainActivity::class.java))
        requireActivity().navigateTo(MainActivity::class.java)
        //requireContext().startActivity(Intent(requireContext(),MainActivity::class.java))
    }
}













